package com.xiaoqi.api.controller.admin;

import com.xiaoqi.api.constant.StatusConst;
import com.xiaoqi.api.dto.SchoolCenterDTO;
import com.xiaoqi.api.exception.ParamException;
import com.xiaoqi.api.query.SchoolCenterQuery;
import com.xiaoqi.api.service.SchoolCenterService;
import com.xiaoqi.api.utlis.PageResult;
import com.xiaoqi.api.utlis.Result;
import com.xiaoqi.api.vo.ProjectVO;
import com.xiaoqi.api.vo.SchoolCenterVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController()
@RequestMapping("/admin/school")
public class SchoolCenterController {
    @Resource
    private SchoolCenterService schoolCenterService;

    @ApiOperation("查询学校")
    @GetMapping
    public Result<List<SchoolCenterVO>> find(SchoolCenterQuery schoolCenterQuery){ //categoryQuery封装了查询的条件
        PageResult<SchoolCenterVO> page = schoolCenterService.find(schoolCenterQuery);

        return Result.ok("成功",page.getList(),page.getTotal());
    }

    @ApiOperation("查询项目接口")
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        SchoolCenterVO schoolCenterVO = schoolCenterService.findById(id);
        return Result.ok("成功",schoolCenterVO);
    }

    @ApiOperation("添加学校")
    @PostMapping
    public Result add(@RequestBody SchoolCenterDTO schoolCenterDTO){
        schoolCenterService.add(schoolCenterDTO);
        return Result.ok("添加成功");
    }

    @ApiOperation("编辑项目")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, SchoolCenterDTO schoolCenterDTO){
        schoolCenterService.update(id,schoolCenterDTO);
        return Result.ok("修改成功！");
    }

    @ApiOperation("根据ID删除学校")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除学校的id位："+id);
        schoolCenterService.deleteById(id);
        return Result.ok("删除成功！");
    }

    @ApiOperation("启用禁用账号")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable Integer id,String status) throws ParamException {
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            schoolCenterService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            schoolCenterService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }

}
